package com.zimmur.app.mocolink.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.app.mocolink.dao.TopicGoodsMapper;
import com.zimmur.app.mocolink.dao.TopicMapper;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.form.TopicForm;
import com.zimmur.app.mocolink.domain.model.Topic;
import com.zimmur.app.mocolink.domain.model.TopicGoods;
import com.zimmur.app.mocolink.domain.model.TopicGoodsExample;
import com.zimmur.app.mocolink.domain.param.TopicParamForManage;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.TopicInfo;
import com.zimmur.app.mocolink.domain.vo.TopicVo;
import com.zimmur.app.mocolink.service.ITopicService;

@Service
public class TopicServiceImpl implements ITopicService{
	private static final Logger log = LoggerFactory.getLogger(TopicServiceImpl.class);
	@Autowired
	private TopicMapper topicMapper;
	@Autowired
	private TopicGoodsMapper topicGoodsMapper;
	@Override
	public TopicVo selectTopicVoById(Integer topicId) {
		TopicVo topicVo;
		Topic topic = topicMapper.selectByPrimaryKey(topicId);
		if(topic!= null){
			topicVo = new TopicVo();
			topicVo.setTopicId(topicId);
			topicVo.setTitle(topic.getTitle());
			topicVo.setContent(topic.getContent());
			topicVo.setCoverImage(topic.getCoverImage());
			topicVo.setGoodsTitle(topic.getGoodsTitle());
			List<TopicGoods> topicGoodsList = topicGoodsMapper.selectByTopicId(topicId);
			topicVo.setGoodsList(topicGoodsList);
			topic.setClickQuantity(topic.getClickQuantity()+1);
			topicMapper.updateByPrimaryKeySelective(topic);
		}else{
			topicVo=null;
		}
		return topicVo;
	}

	@Override
	public Topic selectById(Integer topicId) {
		return topicMapper.selectByPrimaryKey(topicId);
	}

	@Override
	public PageUtil<TopicInfo> queryTopicInfo(int pageNo) {
		PageUtil<TopicInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		List<TopicInfo> topicList= topicMapper.selectCheckedTopic(page.getPageBeginNo(), page.getPageSize());
		int count = topicMapper.countCheckedTopic();
		page.setItemList(topicList);
		page.setTotalRecord(count);
		return page;
	}

	@Override
	public PageUtil<Topic> queryByParam(TopicParamForManage param) {
		PageUtil<Topic> page = new PageUtil<>();
		List<Topic> topicList= topicMapper.selectByParam(param);
		int count = topicMapper.countByParam(param);
		page.setPageNo(param.getPageNo());
		page.setPageSize(param.getPageSize());
		page.setItemList(topicList);
		page.setTotalRecord(count);
		return page;
	}


	@Override
	public int checkStatus(Integer topicId, Integer status) {
		Topic topic = topicMapper.selectByPrimaryKey(topicId);
		if(topic == null){
			return -1;
		}
		if(status == Constant.Topic_Check_Status_Pass){
			topic.setCheckStatus(Constant.Topic_Check_Status_Pass);
		}else if(status == Constant.Topic_Check_Status_Refuse){
			topic.setCheckStatus(Constant.Topic_Check_Status_Refuse);
		}
		int flag = topicMapper.updateByPrimaryKeySelective(topic);
		return flag;
	}

	@Override
	public List<TopicGoods> selectDetailByTopicId(Integer topicId) {
		List<TopicGoods> list = topicGoodsMapper.selectByTopicId(topicId); 
		return list;
	}
	@Transactional
	@Override
	public int saveTopic(TopicForm form) {
		int flag =0;
		Topic topic = new Topic();
		
		topic.setContent(form.getContent());
		topic.setCoverImage(form.getCoverImage());
		topic.setGoodsTitle(form.getGoodsTitle());
		topic.setTag(form.getTag());
		topic.setTitle(form.getTitle());
		topic.setTopicType(form.getTopicType());
		Integer topicId=form.getTopicId();
		if(topicId!=null && topicId>0){
			topic.setId(form.getTopicId());
			topic.setUpdatorId(form.getAdminId());
			topic.setUpdatorName(form.getAdminName());
			topic.setUpdateTime(DateUtils.getNow());
			flag = topicMapper.updateByPrimaryKeySelective(topic);
		}else{
			topic.setCheckStatus(Constant.Topic_Check_Status_Wait);
			topic.setClickQuantity(0);
			topic.setCreatorId(form.getAdminId());
			topic.setCreatorName(form.getAdminName());
			topic.setCreateTime(DateUtils.getNow());
			flag = topicMapper.insertSelective(topic);
			if(flag>0){
				topicId=topic.getId();
			}
		}
		List<TopicGoods> topicGoodsList = form.getTopicGoodsList();
		if(!StringUtils.isBlank(form.getDelIds())){
			List<Integer> ids = new ArrayList<>();
			String[] idArray = form.getDelIds().split(",");
			for(String idStr:idArray){
				ids.add(Integer.valueOf(idStr));
			}
			if(ids.size()>0){
				TopicGoodsExample example = new TopicGoodsExample();
				example.createCriteria().andIdIn(ids);
				topicGoodsMapper.deleteByExample(example);
			}
		}
		if(topicGoodsList!=null && topicGoodsList.size()>0){
			for(TopicGoods topicGoods:topicGoodsList){
				topicGoods.setTopicId(topicId);
				if(topicGoods.getId()!=null&& topicGoods.getId()>0){
					topicGoodsMapper.updateByPrimaryKeySelective(topicGoods);
				}else{
					topicGoodsMapper.insertSelective(topicGoods);
				}
			}
		}
		return flag;
	}
	@Transactional
	@Override
	public int deleteTopic(Integer topicId) {
		int flag = topicMapper.deleteByPrimaryKey(topicId);
		if(flag>0){
			TopicGoodsExample example = new TopicGoodsExample();
			example.createCriteria().andTopicIdEqualTo(topicId);
			topicGoodsMapper.deleteByExample(example);
		}
		return flag;
	}

}
